package www.ym.com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.CustomException;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.dto.DishDto;
import www.ym.com.entity.Category;
import www.ym.com.entity.Dish;
import www.ym.com.entity.DishFlavor;
import www.ym.com.entity.Note;
import www.ym.com.service.CategoryService;
import www.ym.com.service.DishFlavorService;
import www.ym.com.service.DishService;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
//
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
//

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping("/add")
    public R<String> addDish(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavor(dishDto);
        return R.success(null, "新增成功");
    }

    /**
     * 修改菜品信息
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    public R<String> updateDish(@RequestBody DishDto DishDto) {
        dishService.updateWithFlavor(DishDto);
        return R.success(null, "修改成功");
    }

    /**
     * 通过id查询菜系分类对应的菜品
     * @param id
     * @return
     */
    @GetMapping("/byId")
    public R<List<Dish>> byIdDish(Long id){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);
        List<Dish> list = dishService.list(wrapper);
        return  R.success(list,"");
    }

    @PostMapping("/status/{status}")
    //起售、停售
    public R<String> upState(@PathVariable Integer status, @RequestBody List<Long> id ){
        log.info("id"+id);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,id);
        List<Dish> list = dishService.list(wrapper);
        if(list!=null){
            for(Dish dish:list) {
                dish.setStatus(status);
                dishService.updateById(dish);
            }

        }
        return  R.success(null,"售卖状态修改成功");
    }


    /**
     * 删除菜品
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R<String> del(Long id) {
        Dish byId = dishService.getById(id);
        if (byId.getStatus() == 1) {
            throw new CustomException("菜品状态为启售，无法删除");
        }

        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        dishFlavorService.remove(wrapper);
        dishService.removeById(id);
        return R.success(null, "删除成功");
    }

    /**
     * 查询
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/list")
    public R<Page> search(@RequestBody JSONObject jsonObject) {
        log.info("已经进入菜品" + 111);

        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));
        //每页显示数
        Long pageSize = Long.valueOf(String.valueOf(jsonObject.get("pageSize")));
        //售卖状态
        Integer status=null;
        if(String.valueOf(jsonObject.get("status"))==""){
             status=null;
        }else{
             status = Integer.valueOf(String.valueOf(jsonObject.get("status")));

        }
        //分类名称
        String name= String.valueOf(jsonObject.get("name"));
//
        log.info("当前页为" + pageNum);
        log.info("每页显示数" + pageSize);

        Page<Dish> dishPage = new Page<>(pageNum, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!="",Dish::getName,name).eq(status!=null,Dish::getStatus,status);

        dishService.page(dishPage, wrapper);
        List<Dish> records = dishPage.getRecords();
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");

        List<DishDto> listTotal= records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Category one = categoryService.getById(item.getCategoryId());
            if(one!=null){
                dishDto.setCategoryName(one.getCategoryName());
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(listTotal);
        log.info("dishDtoPage"+dishDtoPage);
        System.out.println("数据为：" + dishDtoPage.getRecords());
        System.out.println("总数为：" + dishDtoPage.getTotal() + ",总页数为：" + dishDtoPage.getPages());
        System.out.println("当前页为：" + dishDtoPage.getCurrent() + ",每页限制：" + dishDtoPage.getSize());
        return R.success(dishDtoPage,null);
    }

    /**
     * 菜品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public R<DishDto> detail(Long id) {

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(Dish::getId, id);
        Dish one = dishService.getOne(wrapper);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(one,dishDto);
        Category byId = categoryService.getById(one.getCategoryId());
        dishDto.setCategoryName(byId.getCategoryName());

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavorItemList(list);

        return R.success(dishDto,null);
    }

    /**
     * 查询启售的菜系菜品
     * @param dish
     * @return
     */
    @PostMapping("/byIdStartSell")
    public R<List<Dish>> dish(@RequestBody Dish dish){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId())
                .eq(dish.getStatus()!=null,Dish::getStatus,dish.getStatus());
        List<Dish> list = dishService.list(wrapper);
        return R.success(list,null);
    }


    /**
     *
     *     通过菜品查询菜系
     * @return
     */
    @GetMapping("/cuisine")
    public R<Category> getDish(String name){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(name!=null,Dish::getName,name);
        Dish dish = dishService.getOne(wrapper);
        if (dish==null){
            throw new CustomException("查无此菜品");
        }
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish!=null,Category::getId,dish.getCategoryId());
        Category category = categoryService.getOne(queryWrapper);
        return R.success(category,null);
    }


    /**
     * 客户端显示起售菜品
     * @return
     */
    @GetMapping("/sellDishs")
     public R<List<Dish>> getStartDish(){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(wrapper);
        return R.success(list,"");
    }

}
